package net.huashitong.ssydt.provider.pk.service;


import net.huashitong.ssydt.base.dao.LongMapper;
import net.huashitong.ssydt.base.service.AbstractLongService;
import net.huashitong.ssydt.pk.constant.PkConsts;
import net.huashitong.ssydt.pk.entity.SsPkGameRule;
import net.huashitong.ssydt.pk.entity.SsPkYmdxMatchRecord;
import net.huashitong.ssydt.provider.pk.mapper.SsPkYmdxMatchRecordMapper;
import net.huashitong.ssydt.provider.utils.date.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.12.12
 */
@Service
@Transactional
public class SsPkYmdxMatchRecordServiceImpl extends AbstractLongService<SsPkYmdxMatchRecord> implements SsPkYmdxMatchRecordService {

    private SsPkYmdxMatchRecordMapper mapper;

    private SsPkGameRuleService ssPkGameRuleService;

    @Autowired
    public SsPkYmdxMatchRecordServiceImpl(SsPkYmdxMatchRecordMapper mapper,
                                          SsPkGameRuleService ssPkGameRuleService) {
        this.mapper = mapper;
        this.ssPkGameRuleService = ssPkGameRuleService;
    }

    @Override
    protected LongMapper<SsPkYmdxMatchRecord> getLongMapper() {
        return this.mapper;
    }

    @Override
    public SsPkYmdxMatchRecord getNowYmdxMatch() {
        SsPkYmdxMatchRecord ssPkYmdxMatchRecord = this.mapper.getNotHistory();
        if (ssPkYmdxMatchRecord == null
                || DateUtils.getDateTime().compareTo(ssPkYmdxMatchRecord.getEndTime()) >= 0) {
            if (ssPkYmdxMatchRecord != null) {
                ssPkYmdxMatchRecord.setHistoryId(ssPkYmdxMatchRecord.getId());
                this.updateByPrimaryKey(ssPkYmdxMatchRecord);
            }
            String today = DateUtils.getDate();
            String time = "12:00:00";
            SsPkGameRule ssPkGameRule = ssPkGameRuleService.getByType(PkConsts.RuleType.YMDX);

            ssPkYmdxMatchRecord = new SsPkYmdxMatchRecord();
            BeanUtils.copyProperties(ssPkGameRule, ssPkYmdxMatchRecord);
            ssPkYmdxMatchRecord.setStartTime(String.format("%s %s", today, time));
            ssPkYmdxMatchRecord.setEndTime(String.format("%s %s", DateUtils.getPastDayStr(today, 1), time));
            ssPkYmdxMatchRecord.setHistoryId(0L);
            ssPkYmdxMatchRecord.setIsFinal((byte) 0);
            ssPkYmdxMatchRecord.setIsSettlement((byte) 0);
            this.insertSelective(ssPkYmdxMatchRecord);
        }
        return ssPkYmdxMatchRecord;
    }

    @Override
    public Long getLastMatchRecordId(Long matchRecordId) {
        return this.mapper.getLastMatchRecordId(matchRecordId);
    }

    @Override
    public List<SsPkYmdxMatchRecord> findNonFinal() {
        return this.mapper.findNonFinal(this.getNowYmdxMatch().getId());
    }
}
